<?php
/**
 * Core_Personal_PasswordChangeForm
 * 
 * @author naspeh
 * @version $Id: PasswordChangeForm.php 659 2008-06-15 11:34:26Z naspeh $
 */
class Core_Personal_PasswordChangeForm extends Core_Form 
{
    /**
     * Инициализация
     *
     * @return Core_Personal_PasswordChangeForm
     */
    public function init()
    {
        $this->addElement('password', 'password_old', array(
            'required' => true,
            'label'    => 'Старый пароль:',
            'attribs'  => array(
                'class' => 'text span-4',
            ),
            'validators' => array(
                new Core_Validate_OldPasswordConfirm()
            )
        ));
        $this->addElement('password', 'password', array(
            'required' => true,
            'label'    => 'Новый пароль:',
            'attribs'  => array(
                'class' => 'text span-4',
            )
        ));
        $this->addElement('password', 'password_confirm', array(
            'required' => true,
            'label'    => 'Подтверждение пароля:',
            'attribs'  => array(
                'class' => 'text span-4',
            )
        ));
        $this->addElement('button', 'submit', array(
            'type'     => 'submit',
            'label'    => 'Сохранить',
            'attribs'  => array(
                'class' => 'positive',
                'icon'  => 'tick'
            )
        ));
        
        return $this;
    }
    /**
     * Вторичная валидация
     *
     * @return Core_Personal_PasswordChangeForm
     */
    protected function _setPostValid()
    {
        $this->getElement('password_confirm')->addValidator(new Core_Validate_PasswordConfirm());
        return $this;
    }
    /**
     * Запускает форму на выполнение
     *
     * @return Core_Personal_PasswordChangeForm
     */    
    public function process()
    {
        $rolesTable = new Core_Table_Roles();
        $values = $this->getValues(true);
        $values = array(
            'password' => md5($values['password'])
        );
        $rolesTable->update($values, 'id = ' . Core_Auth::get('id'));
        Core_Auth::init();
        Core_Message::add('Смена пароля прошла успешно.');  
        return $this;     
    }
}
